package com.example.myapplication.mainactivity.presenter

import android.view.Gravity
import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.myapplication.R
import com.example.myapplication.baseview.MainView
import com.example.myapplication.ben.HomeData
import com.example.myapplication.model.CallBack
import com.example.myapplication.model.CommentModel
import per.goweii.anylayer.Align
import per.goweii.anylayer.AnyLayer


class MainPresenter : BaseMvpPresenter<MainView?>(), IMainPresenter {

    override fun initData() {}

    override fun initSearchData(search: String, pager: Int) {
        CommentModel.getInstant().getHomeData(search, pager, object : CallBack<HomeData> {
            override fun onSuccess(data: HomeData, isSuccess: Boolean, msg: String) {
                mvpView!!.initHomeData(data.list)
            }

            override fun onFailure(fail: String) {
                mvpView!!.showErr(fail)
            }
        })
    }

    fun showPP(v: View,a:FragmentActivity,onEnter:(RecyclerView.LayoutManager,Int) ->Unit) {
        AnyLayer.popup(v)
                .align(Align.Direction.VERTICAL, Align.Horizontal.TO_RIGHT, Align.Vertical.BELOW, true)
                .outsideInterceptTouchEvent(true)
                .backgroundDimDefault()
                .gravity(Gravity.RIGHT)
                .contentView(R.layout.pp_home_layout)
                .bindData {layer ->
                    layer.getView<View>(R.id.item0).setOnClickListener {
                        onEnter(LinearLayoutManager(a),0)
                        layer.dismiss()
                    }
                    layer.getView<View>(R.id.item1).setOnClickListener {
                        onEnter(StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL),1)
                        layer.dismiss()
                    }
                    layer.getView<View>(R.id.item2).setOnClickListener {
                        onEnter(GridLayoutManager(a,3),2)
                        layer.dismiss()
                    }
                }.show()
    }
}